package com.stenio.fancat.service;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stenio.fancat.domain.UserImage;
import com.stenio.fancat.mapper.UserImageMapper;
import com.stenio.fancat.util.IOUtil;

/**
 * 用户图片模块服务实现类
 */
@Service
public class UserImageService extends ServiceImpl<UserImageMapper, UserImage> {

	@Autowired
	private UserImageMapper userImageMapper;

	/**
	 * 通过用户ID获取图片信息
	 * 
	 * @param userImage
	 *            用户图片实体类
	 * 
	 * @return UserBO对象
	 */
	public UserImage queryUserImageByCondition(Integer userId, Integer imgType) {
		if (null == userId) {
			throw new RuntimeException("参数不能为空");
		}
		// 通过用户ID和图片类型查询
		UserImage entity = new UserImage();
		entity.setUserId(userId);
		entity.setImgType(imgType == null ? 0 : imgType);
		userImageMapper.selectOne(entity);
		return userImageMapper.selectOne(entity);
	}

	/**
	 * 保存用户图片信息
	 * 
	 * @param userId
	 *            用户ID
	 * @param ImgType
	 *            图片类型，0为icon
	 * @param imageURL
	 *            图片来源链接
	 */
	public void saveUserImage(Integer userId, Integer ImgType, String imageURL) {
		UserImage userImage = new UserImage();
		userImage.setUserId(userId);
		userImage.setImgType(ImgType);
		try {
			userImage.setImage(IOUtil.toByteArray(imageURL));
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("图片处理失败");
		}
		userImageMapper.insertSelective(userImage);
	}

}
